/**
 * 格式化
 */
export function formatDate (date, fmt) {
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'H+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    }
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + ''
            fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
        }
    }
    return fmt
}

/**
 * 今天
 */
export function today() {
	return formatDate(new Date(), 'yyyy-MM-dd')
}

/**
 * 昨天
 */
export function yesterday() {
	let today = new Date();
	today.setDate(today.getDate() - 1);
	return formatDate(today, 'yyyy-MM-dd')
}

/**
 * 本周
 */
export function week() {
	let day1 = new Date();
	day1.setDate(day1.getDate() - (day1.getDay()-1));
	
	let day2 = new Date();
	day2.setDate(day2.getDate() + (7 - day2.getDay()))

	return [formatDate(day1, 'yyyy-MM-dd'), formatDate(day2, 'yyyy-MM-dd')]
}

/**
 * 本月
 */
export function month() {
	let day1 = new Date();
	day1.setDate(1)
	
	let day2 = new Date()
	day2.setMonth(day2.getMonth() + 1)
	day2.setDate(1);
	let monthDays = (day2 - day1) / (1000 * 60 * 60 * 24);
	
	let day3 = new Date()
	day3.setDate(monthDays)
	
	return [formatDate(day1, 'yyyy-MM-dd'), formatDate(day3, 'yyyy-MM-dd')]
}
/**
 * 上月
 */
export function lastMonth() {
	let day1 = new Date();
	day1.setMonth(day1.getMonth() - 1)
	day1.setDate(1)
	
	let day2 = new Date()
	day2.setDate(1);
	let monthDays = (day2 - day1) / (1000 * 60 * 60 * 24);
	
	let day3 = new Date()
	day3.setMonth(day3.getMonth() - 1)
	day3.setDate(monthDays)
	
	return [formatDate(day1, 'yyyy-MM-dd'), formatDate(day3, 'yyyy-MM-dd')]
}
function padLeftZero (str) {
    return ('00' + str).substr(str.length)
}
